package com.example.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.school.entity.Student;
import com.example.school.service.IStudentService;
import com.example.school.utils.JsonResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丁少华
 * @since 2023-12-14
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    @Operation(summary = "新增")
    @PostMapping
    private JsonResult<Boolean> save(@RequestBody Student record) {
        boolean result = studentService.save(record);
        return JsonResult.success(result);
    }


    @Operation(summary = "列表")
    @GetMapping
    private JsonResult<IPage<Student>> getList(Integer current, Integer size) {
        if(ObjectUtils.isEmpty(current) || ObjectUtils.isEmpty(size)){ //如果没有分页
            List<Student> moods = studentService.list();
            return JsonResult.success(moods);
        }else{
            IPage<Student> moods = studentService.page(new Page(current, size));
            return JsonResult.success(moods);
        }
    }


    @Operation(summary = "更新")
    @PutMapping
    private JsonResult<Boolean> updateById(@RequestBody Student record) {
        boolean result = studentService.updateById(record);
        return JsonResult.success(result);
    }


    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    private JsonResult<Boolean> removeById(@PathVariable int id){
        boolean result = studentService.removeById(id);
        return JsonResult.success(result);
    }
}
